﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZedGraph.WindowsFormsApp
{
    /// <summary>
    /// 启动窗体
    /// </summary>
    public partial class FormStartup : Form
    {
        /// <summary>
        /// 图表控件
        /// </summary>
        private ZedGraphControl _zedGraphControl;

        /// <summary>
        /// 随机数
        /// </summary>
        private Random _random;

        /// <summary>
        /// 初始化启动窗体
        /// </summary>
        public FormStartup()
        {
            InitializeComponent();
            _zedGraphControl = new ZedGraphControl();
            tableLayoutPanel1.Controls.Add(_zedGraphControl);
            _zedGraphControl.Dock = DockStyle.Fill;
            _random = new Random();
        }

        /// <summary>
        /// 按钮1 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            _zedGraphControl.GraphPane.CurveList.Clear();
            _zedGraphControl.GraphPane.GraphObjList.Clear();

            _zedGraphControl.GraphPane.Title.Text = "基础折线图";
            _zedGraphControl.GraphPane.XAxis.Title.Text = "X 轴";
            _zedGraphControl.GraphPane.YAxis.Title.Text = "Y 轴";

            var list = new PointPairList();
            list.Add(10, _random.Next(300));
            list.Add(15, _random.Next(300));
            list.Add(20, _random.Next(300));
            list.Add(25, _random.Next(300));
            list.Add(30, _random.Next(300));

            _zedGraphControl.GraphPane.AddCurve("数据", list, Color.Red, SymbolType.Circle);
            _zedGraphControl.AxisChange();
            _zedGraphControl.Refresh();
        }

        /// <summary>
        /// 按钮2 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            _zedGraphControl.GraphPane.CurveList.Clear();
            _zedGraphControl.GraphPane.GraphObjList.Clear();

            // 设置图表标题和坐标轴标签
            _zedGraphControl.GraphPane.Title.Text = "销售统计柱状";
            _zedGraphControl.GraphPane.XAxis.Title.Text = "月份";
            _zedGraphControl.GraphPane.YAxis.Title.Text = "销售额（万元）";

            // 设置图表背景色
            //_zedGraphControl.GraphPane.Fill = new Fill(Color.LightBlue, Color.White, 90F);

            // 设置图表边框
            //_zedGraphControl.GraphPane.Border.IsVisible = true;
            //_zedGraphControl.GraphPane.Border.Color = Color.Black;
            //_zedGraphControl.GraphPane.Border.Width = 2;

            // 设置表格区域背景色
            //_zedGraphControl.GraphPane.Chart.Fill = new Fill(Color.LightYellow);

            // 设置图例样式
            //_zedGraphControl.GraphPane.Legend.FontSpec.Size = 12;
            //_zedGraphControl.GraphPane.Legend.FontSpec.Family = "微软雅黑";
            //_zedGraphControl.GraphPane.Legend.Fill = new Fill(Color.White);

            string[] months = { "一月", "二月", "三月", "四月", "五月" };
            double[] salesA = { _random.Next(300), _random.Next(300), _random.Next(300), _random.Next(300), _random.Next(300) };
            double[] salesB = { _random.Next(300), _random.Next(300), _random.Next(300), _random.Next(300), _random.Next(300) };

            var barA = _zedGraphControl.GraphPane.AddBar("产品A", null, null, Color.Blue);
            barA.Bar.Fill = new Fill(Color.Blue, Color.LightBlue, 45.0f);
            for (var i = 0; i < months.Length; i++)
            {
                barA.AddPoint(i + 1, salesA[i]);
            }

            var barB = _zedGraphControl.GraphPane.AddBar("产品B", null, null, Color.Red);
            barB.Bar.Fill = new Fill(Color.Red, Color.Pink, 45.0f);
            for (var i = 0; i < months.Length; i++)
            {
                barB.AddPoint(i + 1, salesB[i]);
            }

            // 设置 X 轴为文本类型
            _zedGraphControl.GraphPane.XAxis.Type = AxisType.Text;
            _zedGraphControl.GraphPane.XAxis.Scale.TextLabels = months;

            // 设置 X 轴范围
            _zedGraphControl.GraphPane.XAxis.Scale.Min = 0.5;
            _zedGraphControl.GraphPane.XAxis.Scale.Max = months.Length + 0.5;

            // 添加数据标签
            for (int i = 0; i < months.Length; i++)
            {
                double x = i + 1;
                double yA = salesA[i];
                double yB = salesB[i];

                // 产品A的标签
                TextObj textA = new TextObj(yA.ToString(), x - 0.25, yA + 5, CoordType.AxisXY2Scale, AlignH.Center, AlignV.Bottom);
                textA.FontSpec.Size = 12;
                textA.FontSpec.Family = "微软雅黑";
                textA.FontSpec.Fill.IsVisible = false;
                textA.FontSpec.Border.IsVisible = false;
                _zedGraphControl.GraphPane.GraphObjList.Add(textA);

                // 产品B的标签
                TextObj textB = new TextObj(yB.ToString(), x + 0.25, yB + 5, CoordType.AxisXY2Scale, AlignH.Center, AlignV.Bottom);
                textB.FontSpec.Size = 12;
                textB.FontSpec.Family = "微软雅黑";
                textB.FontSpec.Fill.IsVisible = false;
                textB.FontSpec.Border.IsVisible = false;
                _zedGraphControl.GraphPane.GraphObjList.Add(textB);
            }

            // 刷新图表
            _zedGraphControl.AxisChange();
            _zedGraphControl.Refresh();
        }

        /// <summary>
        /// 按钮3 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            _zedGraphControl.GraphPane.CurveList.Clear();
            _zedGraphControl.GraphPane.GraphObjList.Clear();

            _zedGraphControl.GraphPane.Title.Text = "2025 年销售区域分布 ($M)";

            // 设置坐标轴标题（饼图不需要显示坐标轴）
            _zedGraphControl.GraphPane.XAxis.IsVisible = false;
            _zedGraphControl.GraphPane.YAxis.IsVisible = false;

            // 设置图例位置和样式
            _zedGraphControl.GraphPane.Legend.IsVisible = false;
            //_zedGraphControl.GraphPane.Legend.Position = LegendPos.Top;
            //_zedGraphControl.GraphPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction, AlignH.Right, AlignV.Top);
            //_zedGraphControl.GraphPane.Legend.FontSpec.Size = 10;

            double[] salesA = { _random.Next(300), _random.Next(300), _random.Next(300), _random.Next(300), _random.Next(300) };

            var sum = salesA.Sum();

            // 添加饼图扇区
            var segment1 = _zedGraphControl.GraphPane.AddPieSlice(salesA[0], Color.Navy, Color.White, 45f, 0.03, "北部");
            var segment2 = _zedGraphControl.GraphPane.AddPieSlice(salesA[1], Color.Purple, Color.White, 45f, 0.03, "东部");
            var segment3 = _zedGraphControl.GraphPane.AddPieSlice(salesA[2], Color.LimeGreen, Color.White, 45f, 0.03, "西部");
            var segment4 = _zedGraphControl.GraphPane.AddPieSlice(salesA[3], Color.OrangeRed, Color.White, 45f, 0.03, "南部");
            var segment5 = _zedGraphControl.GraphPane.AddPieSlice(salesA[4], Color.Goldenrod, Color.White, 45f, 0.03, "中部");

            // 可选：设置每个扇区的标签格式
            segment1.Label.Text = $"北部 ({salesA[0] / sum:P})";
            segment2.Label.Text = $"东部 ({salesA[1] / sum:P})";
            segment3.Label.Text = $"西部 ({salesA[2] / sum:P})";
            segment4.Label.Text = $"南部 ({salesA[3] / sum:P})";
            segment5.Label.Text = $"中部 ({salesA[4] / sum:P})";

            _zedGraphControl.AxisChange();
            _zedGraphControl.Refresh();
        }
    }
}
